package com.zhenyi.youqu.core;

import java.util.HashMap;
import java.util.Map;

import org.hashids.Hashids;
import org.joda.time.DateTime;

import com.cenjiawen.common.IndexController;
import com.cjw.model.DBMap;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.weixin.sdk.api.PaymentApi;
import com.jfinal.weixin.sdk.api.PaymentApi.TradeType;
import com.jfinal.weixin.sdk.kit.IpKit;
import com.jfinal.weixin.sdk.kit.PaymentKit;
import com.utils.PayUtils;
import com.zhenyi.xhc.core.XHCPathUtils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.setting.Setting;

public class OrderUtils {

	protected final String APP_ID = PropKit.get("XHC_APP_ID", "wx54277a43aad4df57");
	protected final String APP_SECRET = PropKit.get("XHC_APP_SECRET", "b0442f55643cfa9309b445ff75945cd9");

	// protected String settingPath = PathUtils.getFile("xhc", "xhc.setting");
	// protected Setting rootSetting = new Setting(settingPath);
	protected final int PAGE_SIZE = 50;
	protected Setting setting = new Setting("xhc.setting");
	protected Hashids hashids = new Hashids("xiaohuoche", 16);
	protected Hashids piaoHashids = new Hashids("piao", 8);
	protected final String SECRET = setting.getStr("SECRET", "wechat", "");
	// protected final String APP_ID = setting.getStr("APP_ID", "wechat", "");
	protected final String PARTNER = setting.getStr("PARTNER", "wechat", "");
	protected final String NOTIFY_URL = setting.getStr("NOTIFY_URL", "wechat", "");
	protected final String REFUND_NOTIFY_URL = setting.getStr("REFUND_NOTIFY_URL", "wechat", "");
	protected final String PARTNER_KEY = setting.getStr("PARTNER_KEY", "wechat", "");
	protected String SSL_PATH = XHCPathUtils.getClassFile("wx6d47db34d18f5e3a", "apiclient_cert.p12");
	protected PayUtils payUtils = new PayUtils(APP_ID, PARTNER, PARTNER_KEY, SSL_PATH);
	protected String HOST = setting.getStr("host", "ticketSetting", "");
	protected String QRCODE_HOST = setting.getStr("qrcode", "ticketSetting", "");
	protected String QRCODE_VERIFY = setting.getStr("verify", "ticketSetting", "");
	protected String AES_KEY = PropKit.get("aesKey", "Bar12345Bar12345");

	protected boolean DEBUG = PropKit.getBoolean("devMode", false);

	private IndexController controller;

	public OrderUtils(IndexController controller) {
		this.controller = controller;
	}

	public String genOrder(String orderNo, String title, String total_fee, String openId, String attach) {

		String nonce_str = RandomUtil.randomString(32);
		String tradeNo = orderNo;

		if (DEBUG) {
			// total_fee = "1";
		}

		Map<String, String> params = new HashMap<String, String>();
		params.put("appid", APP_ID);
		params.put("mch_id", PARTNER);
		params.put("body", title);
		params.put("out_trade_no", tradeNo);
		params.put("total_fee", total_fee);
		String ip = IpKit.getRealIp(controller.getRequest());
		if (StrKit.isBlank(ip)) {
			ip = "127.0.0.1";
		}
		if (DEBUG) {
			ip = "127.0.0.1";
		}
		params.put("spbill_create_ip", ip);
		params.put("trade_type", TradeType.JSAPI.name());
		params.put("nonce_str", nonce_str);
		params.put("notify_url", NOTIFY_URL);
		params.put("openid", openId);
		System.out.println("attach:" + attach);
		if (StrKit.notBlank(attach)) {
			params.put("attach", attach);
		}
		String sign = PaymentKit.createSign(params, PARTNER_KEY);
		params.put("sign", sign);
		String xmlResult = PaymentApi.pushOrder(params);
		Map<String, String> result = PaymentKit.xmlToMap(xmlResult);

		if (DEBUG) {
			LogKit.info(JsonKit.toJson(params));
			LogKit.info(xmlResult);
			LogKit.info("APP_ID: " + APP_ID);
			LogKit.info("PARTNER: " + PARTNER);
			LogKit.info("PARTNER_KEY: " + PARTNER_KEY + " - " + PARTNER_KEY.length());
			LogKit.info("trade_type: " + TradeType.JSAPI.name());
		}
		if (!PayUtils.isXMLSuccess(result)) {
			// renderMsg("支付出错");
			return null;
		}

		String prepay_id = result.get("prepay_id");
		DateTime dateTime = new DateTime();
		String timeStamp = dateTime.toDate().getTime() / 1000 + "";
		String signString = "appId=" + APP_ID + "&nonceStr=" + nonce_str + "&package=prepay_id=" + prepay_id
				+ "&signType=MD5&timeStamp=" + timeStamp + "&key=" + PARTNER_KEY;
		signString = HashKit.md5(signString).toUpperCase();
		DBMap map = new DBMap();
		map.put("result", result);
		map.put("sign", signString);
		map.put("timeStamp", timeStamp);
		map.put("nonce_str", nonce_str);
		// order.setPrepayId(prepay_id);
		// order.update();
		controller.renderResult(map, "pay", 201);
		return prepay_id;

	}
}
